package com.threeonemeeting.activity;

import java.util.Map;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.snda.lib.http.ICallBack;
import com.snda.lib.http.TaskManager;
import com.threeonemeeting.Const;
import com.threeonemeeting.DataCenter;
import com.threeonemeeting.R;
import com.threeonemeeting.activity.ext.MeetingPageListActivity;
import com.threeonemeeting.listview.DragRefreshListView;
import com.threeonemeeting.model.CityInfo;
import com.threeonemeeting.model.MeetingTradesInfo;
import com.threeonemeeting.tab.MultiActivityTab;
import com.threeonemeeting.task.SearchMeetingTask;
import com.threeonemeeting.util.MiscUtil;

public class SearchMeetingListActivity extends MeetingPageListActivity
		implements ICallBack {

	public static final int TAB_ALLCITY = 0;
	public static final int TAB_ALL = 1;
	public static final int TAB_INWEEK = 2;
	public static final int TAB_WEEKEND = 3;
	public static final int TAB_MONTH = 4;
	private int mCurrentTabIndex = TAB_ALL;

	private Button mBtnAllCity = null;
	private Button mBtnAll = null;
	private Button mBtnInWeek = null;
	private Button mBtnWeekEnd = null;
	private Button mBtnMonth = null;

	private int nTradeId = 0;
	private int nSearchType = Const.Search.SEARCH_ALL;
	private int nProvinceId = 0;
	private int nCityId = 0;
	private String strKeywords = "";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ac_search_meeting_list, "所有行业");

		DataCenter dataCenter = DataCenter.getInstance();

		final String[] citys = new String[dataCenter.listCity.size() + 1];

		CityInfo cInfo = new CityInfo();
		cInfo.nProvinceId = 0;
		cInfo.nCityId = 0;
		cInfo.strCityName = "所有城市";
		cInfo.nId = 0;
		citys[0] = cInfo.strCityName;

		for (int i = 1; i < citys.length; i++) {
			cInfo = dataCenter.listCity.get(i - 1);
			if (cInfo == null)
				continue;

			citys[i] = cInfo.strCityName;
		}

		Bundle b = getIntent().getExtras();

		nTradeId = b.getInt("tradeid");
		nSearchType = b.getInt("type");
		nProvinceId = b.getInt("provinceid");
		nCityId = b.getInt("cityid");
		strKeywords = b.getString("keyword");

		String strTitle = b.getString("title");
		TextView nameTextView = (TextView) findViewById(R.id.title);
		nameTextView.setText(strTitle);

		// 设置各个按钮的响应
		mBtnAllCity = (Button) findViewById(R.id.allcity_button);
		mBtnAll = (Button) findViewById(R.id.all_button);
		mBtnInWeek = (Button) findViewById(R.id.inweek_button);
		mBtnWeekEnd = (Button) findViewById(R.id.weekend_button);
		mBtnMonth = (Button) findViewById(R.id.month_button);

		if (nCityId != 0) {
			CityInfo currentCity = dataCenter.listCity.getCityInfo(nProvinceId,
					nCityId);
			if (currentCity != null) {
				mBtnAllCity.setText(currentCity.strCityName);
			}
		}

		mBtnAllCity.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {

				new AlertDialog.Builder(
						((MultiActivityTab) SearchMeetingListActivity.this
								.getParent())).setTitle("选择城市")
						.setItems(citys, new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {

								String strCityName = citys[which];
								if (strCityName.equalsIgnoreCase("所有城市") == true) {
									nProvinceId = 0;
									nCityId = 0;
								} else {
									CityInfo cInfo = DataCenter.getInstance().listCity
											.getCityInfo(strCityName);
									nProvinceId = cInfo.nProvinceId;
									nCityId = cInfo.nCityId;
								}

								mBtnAllCity.setText(strCityName);

								meetingListView.reset();
								searchMeeting(1);
							}
						}).create().show();

			}

		});

		mBtnAll.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				setCurrentTab(TAB_ALL);
				nSearchType = Const.Search.SEARCH_ALL;

				meetingListView.reset();
				searchMeeting(1);
			}

		});

		mBtnInWeek.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				setCurrentTab(TAB_INWEEK);
				nSearchType = Const.Search.SEARCH_INWEEK;

				meetingListView.reset();
				searchMeeting(1);
			}

		});

		mBtnWeekEnd.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				setCurrentTab(TAB_WEEKEND);
				nSearchType = Const.Search.SEARCH_WEEKEND;

				meetingListView.reset();
				searchMeeting(1);
			}

		});

		mBtnMonth.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				setCurrentTab(TAB_MONTH);
				nSearchType = Const.Search.SEARCH_MONTH;

				meetingListView.reset();
				searchMeeting(1);
			}
		});

		// 选择其余行业的按钮
		Button btnSelectTrades = (Button) findViewById(R.id.btn_changetrade);
		OnClickListener selectTradesBtnClickListener = new OnClickListener() {

			public void onClick(View v) {

				final DataCenter dataCenter = DataCenter.getInstance();
				final String[] trades = new String[dataCenter.listTrades.size()];
				for (int i = 0; i < dataCenter.listTrades.size(); i++) {

					MeetingTradesInfo tradesInfo = dataCenter.listTrades.get(i);
					if (tradesInfo == null)
						continue;

					trades[i] = tradesInfo.strCategoryName;
				}

				new AlertDialog.Builder(
						((MultiActivityTab) SearchMeetingListActivity.this
								.getParent()))
						.setTitle("选择其余行业")
						.setItems(trades,
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int which) {

										// // 打开对应的会讯专题
										MeetingTradesInfo tradesInfo = dataCenter.listTrades
												.get(which);
										if (tradesInfo == null)
											return;

										meetingListView.reset();

										nTradeId = tradesInfo.nCategoryId;
										searchMeeting(1);

										TextView title = (TextView) findViewById(R.id.title);
										if (title != null) {
											title.setText(tradesInfo.strCategoryName);
										}

									}
								}).create().show();
			}
		};
		btnSelectTrades.setOnClickListener(selectTradesBtnClickListener);

		// ListView
		meetingListView
				.setOnLoadMoreListener(new DragRefreshListView.OnLoadMoreListener() {
					public void onLoadMore(int nNextPage) {
						searchMeeting(nNextPage);
						showLoadingMoreLayout(true);
					}
				});

		meetingListView
				.setOnRefreshListener(new DragRefreshListView.OnRefreshListener() {
					public void onRefresh() {
						meetingListView.reset();
						searchMeeting(1, true);
					}
				});

		searchMeeting(1);
		setCurrentTab(TAB_ALL);

		MiscUtil.checkCity(this, null);
		MiscUtil.checkDepartment(this, null);
	}

	protected void setCurrentTab(int tabIndex) {
		setTabButtonStatus(mCurrentTabIndex, false);
		setTabButtonStatus(tabIndex, true);
		mCurrentTabIndex = tabIndex;
	}

	private void setTabButtonStatus(int tabIndex, boolean focus) {

		switch (tabIndex) {
		case TAB_ALLCITY:
			if (focus) {
				mBtnAllCity.setTextAppearance(this,
						android.R.style.TextAppearance_Theme);
				mBtnAllCity.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.btn_style_gray_4_round_selected));
			} else {
				mBtnAllCity.setTextAppearance(this,
						android.R.style.TextAppearance_Theme);
				mBtnAllCity.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.btn_style_gray_4_round_default));
			}
			break;

		case TAB_ALL:
			if (focus) {
				mBtnAll.setTextAppearance(this,
						android.R.style.TextAppearance_Theme);
				mBtnAll.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.btn_style_gray_left_round_selected));
			} else {
				mBtnAll.setTextAppearance(this,
						android.R.style.TextAppearance_Theme);
				mBtnAll.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.btn_style_gray_left_round_default));
			}
			break;

		case TAB_INWEEK:
			if (focus) {
				mBtnInWeek.setTextAppearance(this,
						android.R.style.TextAppearance_Theme);
				mBtnInWeek.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.btn_style_gray_no_round_selected));
			} else {
				mBtnInWeek.setTextAppearance(this,
						android.R.style.TextAppearance_Theme);
				mBtnInWeek.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.btn_style_gray_no_round_default));
			}
			break;
		case TAB_WEEKEND:
			if (focus) {
				mBtnWeekEnd.setTextAppearance(this,
						android.R.style.TextAppearance_Theme);
				mBtnWeekEnd.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.btn_style_gray_no_round_selected));
			} else {
				mBtnWeekEnd.setTextAppearance(this,
						android.R.style.TextAppearance_Theme);
				mBtnWeekEnd.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.btn_style_gray_no_round_default));
			}
			break;
		case TAB_MONTH:
			if (focus) {
				mBtnMonth.setTextAppearance(this,
						android.R.style.TextAppearance_Theme);
				mBtnMonth.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.btn_style_gray_right_round_selected));
			} else {
				mBtnMonth.setTextAppearance(this,
						android.R.style.TextAppearance_Theme);
				mBtnMonth.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.btn_style_gray_right_round_default));
			}
			break;

		default:
			break;
		}
	}

	private void searchMeeting(int nPage) {
		searchMeeting(nPage, false);
	}

	private void searchMeeting(int nPage, boolean bRefresh) {

		// 发送请求，请求会议列表
		SearchMeetingTask task = new SearchMeetingTask(
				SearchMeetingListActivity.this, SearchMeetingListActivity.this);
		task.setPages(nPage);
		task.setRefresh(bRefresh);
		Bundle params = new Bundle();
		params.putString("page", String.valueOf(nPage));

		// 搜索参数
		if (nSearchType == Const.Search.SEARCH_INWEEK) {
			params.putString("date", "7");
		} else if (nSearchType == Const.Search.SEARCH_WEEKEND) {
			params.putString("date", "2");
		} else if (nSearchType == Const.Search.SEARCH_MONTH) {
			params.putString("date", "30");
		}

		if (nTradeId != 0) {
			params.putString("industry", String.valueOf(nTradeId));
		}

		if (nProvinceId != 0) {
			params.putString("province", String.valueOf(nProvinceId));
		}

		if (nCityId != 0) {
			params.putString("city", String.valueOf(nCityId));
		}

		if (strKeywords != null && strKeywords.equalsIgnoreCase("") == false) {
			params.putString("key", strKeywords);
		}

		TaskManager.addTask(task, params, false);
	}

	@Override
	public void onSuccessCallBack(Map<String, Object> mapData) {
		super.onSuccessCallBack(mapData);

		TextView norecordTextView = (TextView) findViewById(R.id.norecord_textview);
		if (mAdapter.getCount() == 0) {
			if (mCurrentTabIndex == TAB_INWEEK) {
				norecordTextView.setText("本周暂时没有相关会议");
			} else if (mCurrentTabIndex == TAB_WEEKEND) {
				norecordTextView.setText("本周末暂时没有相关会议");
			} else if (mCurrentTabIndex == TAB_MONTH) {
				norecordTextView.setText("本月末暂时没有相关会议");
			} else if (mCurrentTabIndex == TAB_ALL) {
				norecordTextView.setText("近期暂时没有相关会议");
			}
		}
	}
}
